package org.dstart.draw.cui.components
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	import org.dstart.draw.cglobal.GInitData;
	import org.dstart.draw.cmanage.CSpaceManage;
	import org.dstart.draw.csuper.CSprite;
	import org.dstart.draw.cui.symbol.basic.BWindowBackground;
	import org.dstart.draw.cui.symbol.basic.BWindowTitle;
	import org.dstart.draw.cui.symbol.events.CWinTitleEvent;
	import org.dstart.draw.iface.IDraw;
 	
	public class CWindow extends CSprite implements IDraw
	{
		private var m_winBg:BWindowBackground;	//	背景框
		private var m_winTitle:BWindowTitle;	//	标题按钮组
		private var m_sManage:CSpaceManage;		//	空间管理对象
		
		private var fSprite:Sprite;			//	当前对象的上一级对象
		private var baseRec:Rectangle;		//	基础的对象位置
		
		public function CWindow(spr:Sprite, ww:Number = 400, hh:Number = 300)
		{			
			fSprite = spr;
			
			if(GInitData.BASE_WIDTH == 0 || GInitData.BASE_HEIGHT == 0)
			{
				GInitData.InitData(fSprite);
			}
			
			InitObject();			
			ChangeSize(ww, hh);
		}
		
		//	修改窗体大小
		public function ChangeSize(ww:Number, hh:Number):void
		{
			m_winBg.ChangeSize(ww, hh);						  
			m_winTitle.ChangeSize(m_winBg.rect.width - 1, m_winTitle.rect.height);
			m_winTitle.SetPlace(m_winBg.rect.x, m_winBg.rect.y);
		}
		
		//	返回可用区大小
		public function get rect():Rectangle
		{
			return null;
		}		
		
		//	添加一个新对象
		public function AddIteam(child:DisplayObject):DisplayObject
		{			
			m_sManage.AddObject(child);
			
			return child;
		}
		
		//	删除一个对象从列表中
		public function RemoveIteam(child:DisplayObject):DisplayObject
		{
			m_sManage.RemoveObject(child);
			
			return child;  
		}
		
		//	取得窗体的宽度
		public function get frameWidth():Number
		{
			return m_sManage.rect.width;
		}
		
		//	取得窗体的高度
		public function get frameHeight():Number
		{
			return m_sManage.rect.height;
		}		
				
		//-------------------------------------------------------------
		
		private function InitObject():void
		{
			m_winBg = new BWindowBackground();
			m_winTitle = new BWindowTitle();
			
			var rect:Rectangle = new Rectangle();
			rect.x = m_winBg.rect.x + 1;
			rect.y = m_winBg.rect.y + m_winTitle.rect.height + 1;
			rect.width = m_winBg.rect.width - 2;
			rect.height = m_winBg.rect.height - m_winTitle.rect.height - 2;
			
			m_sManage = new CSpaceManage(rect);
			
			super._root.addChild(m_winBg);
			super._root.addChild(m_winTitle);
			super._root.addChild(m_sManage);
			
			this.cacheAsBitmap = true;
			this.visible = false;
			
			fSprite.stage.addChild(this);
		}
		
		//	对象显示出来以后，设置事件侦听
		protected override function InitDraw():void
		{
			//	窗口拖动事件
			m_winTitle.addEventListener(CWinTitleEvent.START_MOVE, onStartDragWindow);
			m_winTitle.addEventListener(CWinTitleEvent.STOP_MOVE, onStopDragWindow);
			m_winTitle.addEventListener(CWinTitleEvent.MOVE_PRESS, onRushWindows);
			
			//	窗口按钮事件
			m_winTitle.addEventListener(CWinTitleEvent.MAXIMIZE, onMaxWindow);
			m_winTitle.addEventListener(CWinTitleEvent.MINIMIZE, onMinWindow);
			m_winTitle.addEventListener(CWinTitleEvent.RESTORE, onRestoreWindow);
			m_winTitle.addEventListener(CWinTitleEvent.CLOSE, onCloseWindow);
			
			//	全局侦听事件
			this.stage.addEventListener(Event.RESIZE, onReSizeWindow);
		}		
		
		private var lp:Point;
		
		private function onStartDragWindow(e:Event):void
		{
			//var p:Point = this.localToGlobal(new Point(this.parent.x, this.parent.y));
			// lp = new Point(this.stage.mouseX, this.stage.mouseY);
			
			// OpenBitmap();			
			startDrag();
		}
		
		private function onStopDragWindow(e:Event):void
		{
			stopDrag();
			// CloseBitmap();
		}
		
		private function onRushWindows(e:Event):void
		{			
			// this.SetPlace(this.stage.mouseX - lp.x, this.stage.mouseY - lp.y);

			//CloseBitmap();
			//OpenBitmap();
		}
		
		//	点击最大化按钮后触发事件
		private function onMaxWindow(e:Event):void
		{
			baseRec = new Rectangle(x, y, width, height);
			
			ResizeToMax();
		}
		
		private function ResizeToMax():void
		{
			x = (GInitData.BASE_WIDTH - stage.stageWidth)/2 - m_winBg.rect.x;
			y = (GInitData.BASE_HEIGHT - stage.stageHeight)/2 - m_winBg.rect.y;
			
			var ww:Number = stage.stageWidth + m_winBg.rect.x * 2 - 1;
			var hh:Number = stage.stageHeight + m_winBg.rect.y * 2 - 1;
			
			ChangeSize(ww, hh);
		}
		
		//	最小化窗口
		private function onMinWindow(e:Event):void
		{
			
		}
		
		//	还原窗口
		private function onRestoreWindow(e:Event):void
		{
			if(baseRec)
			{
				ChangeSize(baseRec.width, baseRec.height);
				this.x = baseRec.x;
				this.y = baseRec.y;
				baseRec = null;
			}
		}
		
		//	关闭窗口
		private function onCloseWindow(e:Event):void
		{
			// this.Hidden();
		}
		
		//	外部改变窗口大小
		private function onReSizeWindow(e:Event):void
		{
			if(m_winTitle.isMaximize)
			{
				ResizeToMax();
			}
		}
	}
}